<?php
namespace app\model\frontend;

use think\Model;
use think\facade\Db;

/**
 * 自提点模型
 */
class PickupModel extends Model
{
    protected $pk = 'id';
    // 设置当前模型对应的完整数据表名称
    protected $table = 'sh_pick_up';

    /**
     * 获取自提点列表
     * @param array $filter 过滤条件
     * @param int $page 页码
     * @param int $page_num 每页数量
     * @return array
     */
    public static function get_list($filter = [], $page = 1, $page_num = 20)
    {
        global $_W;
        $uniacid = $_W['uniacid'];
        
        $where_arr = [];
        $where_arr[] = ' status=1';
        $where_arr[] = ' uniacid=' . $uniacid;
        
        // 如果有名称筛选
        if (!empty($filter['name'])) {
            $where_arr[] = ' name like "%' . $filter['name'] . '%"';
        }
        
        // 如果有地址筛选
        if (!empty($filter['address'])) {
            $where_arr[] = ' address like "%' . $filter['address'] . '%"';
        }
        
        $where_str = '';
        if (!empty($where_arr)) {
            $where_str = ' where ' . join(' and ', $where_arr);
        }
        
        $sql = 'select * from sh_pick_up' . $where_str;
        $sql .= ' order by id desc';
        $sql .= ' limit ' . ($page - 1) * $page_num . ', ' . $page_num;

        return Db::query($sql);
    }

    /**
     * 获取单个自提点信息
     * @param int $id 自提点ID
     * @return array|null
     */
    public static function single($id)
    {
        $sql = 'select * from sh_pick_up where id=' . intval($id);
        $result = Db::query($sql);
        
        return !empty($result) ? $result[0] : null;
    }

    /**
     * 获取自提点列表（带分页信息）
     * @param array $filter 过滤条件
     * @return array
     */
    public static function getListWithPage($filter = [])
    {
        global $_W;
        $uniacid = $_W['uniacid'];
        
        $where_arr = [];
        $where_arr[] = ' status=1';
        $where_arr[] = ' uniacid=' . $uniacid;
        
        // 如果有名称筛选
        if (!empty($filter['name'])) {
            $where_arr[] = ' name like "%' . $filter['name'] . '%"';
        }
        
        // 如果有地址筛选
        if (!empty($filter['address'])) {
            $where_arr[] = ' address like "%' . $filter['address'] . '%"';
        }
        
        $where_str = '';
        if (!empty($where_arr)) {
            $where_str = ' where ' . join(' and ', $where_arr);
        }
        
        $sql = 'select * from sh_pick_up' . $where_str;
        $sql .= ' order by id desc';
        
        $total_sql = 'select count(id) as total from sh_pick_up' . $where_str;
        
        $page_size = isset($_GET['page_size']) ? intval($_GET['page_size']) : 15;
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $sql .= ' limit ' . ($page - 1) * $page_size . ',' . $page_size;
        
        return [
            'list' => Db::query($sql),
            'page' => [
                'total' => Db::query($total_sql)[0]['total'],
                'page_size' => $page_size,
                'page' => $page,
            ]
        ];
    }
}